package myself.a002StackAndQueue;

import myself.a000util.Logarithm;

import static myself.a000util.NodeCollection.SingleNode;
import static myself.a000util.NodeCollection.DoubleNode;

;

/**
 * @author 叶之越
 * Description
 * Date 2021/1/21
 * Time 19:37
 * Mail 739153436@qq.com
 */
public class ReverseList {
    public static void main(String[] args) {
        Logarithm.NodeList.checkSingleReverse(ReverseList.class, "reverseLinkedList");
        Logarithm.NodeList.checkDoubleReverse(ReverseList.class, "reverseDoubleList");
    }

    // 单链表的反转
    public SingleNode reverseLinkedList(SingleNode head) {
        SingleNode pre = null;
        SingleNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    // 双链表反转
    public DoubleNode reverseDoubleList(DoubleNode head) {
        DoubleNode pre = null;
        while (head != null) {
            head.pre = head.next;
            head.next = pre;
            pre = head;
            head = head.pre;
        }
        return pre;
    }
}
